<?php
require_once LIBS_DIR . 'classes/class.Database.php'; //LIBS_DIR is defined in config.php

class UserSession{
	private $php_session_id;
	private $native_session_id;
	private $logged_in;
	private $user_id;
	private $session_timeout = 600; //10-min time-out
	private $session_lifespan = 3600; //1 hour session
	
	public function __construct(){
		$db = new Database();
		session_set_save_handler(
			array(&$this, '_session_open_method'),
			array(&$this, '_session_close_method'),
			array(&$this, '_session_read_method'),
			array(&$this, '_session_write_method'),
			array(&$this, '_session_destroy_method'),
			array(&$this, '_session_gc_method')
		);
		$strUserAgent = $GLOBALS["HTTP_USER_AGENT"];
		if (!empty($_COOKIE["PHPSESSID"]))
		{
			$this->php_session_id = $_COOKIE["PHPSESSID"];
			//$stmt = "SELECT `id` FROM `user_session` WHERE `ascii_session_id` = '".$this->php_session_id."' AND ((now()-`created`) < '".$this->session_lifespan." seconds') AND `user_agent`='".$strUserAgent."'AND ((now()-`last_impression`) <= '".$this->session_timeout." seconds' OR last_impression IS NULL)";
			$rows = $db->select( ' `sf_user_session` ', " 	`ascii_session_id` = '".$this->php_session_id."'
														 	AND ((now()-`created`) < '".$this->session_lifespan." seconds')
														 	AND `user_agent`='".$strUserAgent."'
														 	AND ((now()-`last_impression`) <= '".$this->session_timeout." seconds'
														 	OR last_impression IS NULL)",
														' `id` ' );	
			if ($rows == 0)
			{
				$failed = 1;
				//Delete from DB
				$db->sql_query(" DELETE FROM `sf_user_session`
								 WHERE (`ascii_session_id` = '".$this->php_session_id."') OR (now()-`created`) > $maxlifetime");
			}
		}
		
	}
}
?>